/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.h2sql;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import yhao.infra.feature.h2sql.mybatis.dialect.H2SqlDialect;
import yhao.infra.service.YhaoInfraServiceProperties;

import java.util.concurrent.ScheduledExecutorService;

/**
 * H2SQL 配置类
 *
 * @author yoara
 */
@ComponentScan("yhao.infra.feature.h2sql.mybatis")
@EnableConfigurationProperties(YhaoH2SqlProperties.class)
public class H2SqlConfig {
    @Bean(name="h2sqlCreateScheduler")
    public ScheduledExecutorFactoryBean h2sqlCreateScheduler(YhaoH2SqlProperties properties){
        ScheduledExecutorFactoryBean scheduledExecutorFactoryBean = new ScheduledExecutorFactoryBean();
        scheduledExecutorFactoryBean.setPoolSize(properties.getDruidScheduledExecutorPoolSize());
        return scheduledExecutorFactoryBean;
    }

    @Bean(name="h2sqlDefaultDataSource",initMethod = "init",destroyMethod = "close")
    public DruidDataSource h2sqlDefaultDataSource(YhaoH2SqlProperties properties, ScheduledExecutorService h2sqlCreateScheduler){
        return ConfigFactory.makeH2sqlDruidDataSource(properties, h2sqlCreateScheduler);
    }

    @Bean(name="h2sqlDataSource")
    public DruidDataSource h2sqlDataSource(DruidDataSource h2sqlDefaultDataSource){
        DruidDataSource defaultDataSource = h2sqlDefaultDataSource;
        //多库切换
        //DynamicDataSource source = new DynamicDataSource();
        //Map<Object, Object> targetDataSources =
        //        ImmutableMap.of("defaultDataSource",defaultDataSource);
        //source.setTargetDataSources(targetDataSources);
        //source.setDefaultTargetDataSource(defaultDataSource);

        return defaultDataSource;
    }

    @Bean(name="h2sqlSqlSessionFactory")
    public SqlSessionFactory h2sqlSqlSessionFactory(
            DruidDataSource h2sqlDataSource, YhaoInfraServiceProperties baseProperties) throws Exception {
        SqlSessionFactory factory = ConfigFactory.
                makeSqlSessionFactory(baseProperties,h2sqlDataSource,H2SqlDialect.class);
        return factory;
    }

    @Bean(name="h2sqlJdbcTemplate")
    public JdbcTemplate h2sqlJdbcTemplate(DruidDataSource h2sqlDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(h2sqlDataSource);
        return jdbcTemplate;
    }

    @Bean(name = "h2sqlTxManager")
    public PlatformTransactionManager h2sqlTxManager(DruidDataSource h2sqlDataSource) {
        return new DataSourceTransactionManager(h2sqlDataSource);
    }
}
